package com.jhmk.jhecis.controller;

import com.jhmk.jhecis.config.job.Job1;
import com.jhmk.jhecis.config.job.RestJob;
import com.jhmk.jhecis.domain.common.R;
import com.jhmk.jhecis.utils.JobUtil;
import com.jhmk.jhecis.utils.JsonUtils;
import com.jhmk.jhecis.dto.ApiJob;
import com.jhmk.jhecis.dto.JobBean;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/quartz")
@Tag( name="操作定时任务接口")
public class QuartzController {
    @Autowired
    private Scheduler scheduler;

    private String JobName = "myJob";


    @GetMapping("/test")
    public String test() {

        return "test";
    }

    @GetMapping("/createapi")
    @Operation(summary = "创建一个调用Api的定时任务")
    public R<String> createApiJob() {
        ApiJob apiData = new ApiJob("","get","http://192.168.103.161:8890/api/DictDept/GetDictDeptList?name=美容中心");
        String str= JsonUtils.objToStr(apiData);
        JobBean jobBean = new JobBean("apijob1", RestJob.class.getName(), "0/10 * * * * ? *",str);
        JobUtil.createJob(scheduler, jobBean);
        return R.ok("创建成功");
    }

    @GetMapping("/create")
    public String createJob() {
        JobBean jobBean = new JobBean(JobName, Job1.class.getName(), "0/2 * * * * ? *","");
        JobUtil.createJob(scheduler, jobBean);
        return "任务创建成功";
    }

    @GetMapping("/pause")
    public String pauseJob() {
        JobUtil.pauseJob(scheduler, JobName);
        return "任务暂停成功";
    }

    @GetMapping("/resume")
    public String resumeJob() {
        JobUtil.resumeJob(scheduler, JobName);
        return "任务恢复成功";
    }

    @GetMapping("/delete")
    public String deleteJob() {
        JobUtil.deleteJob(scheduler, JobName);
        return "任务删除成功";
    }

    /**
     * 执行这个需要先执行create获得JobBena，然后暂停才可以执行这个
     * @return
     */
    @GetMapping("/once")
    public String runOnceJob() {
        JobUtil.runJobOnce(scheduler, JobName);
        return "任务执行一次成功";
    }

    /**
     * 此方法也需要限制性create
     * @return
     */
    @GetMapping("/modify")
    public String modifyJob() {
        JobBean jobBean = new JobBean(JobName, Job1.class.getName(),"0/5 * * * * ? *","");
        JobUtil.modifyJob(scheduler, jobBean);
        return "任务更新成功";
    }
}
